package ink.lovejinhu.leetcode;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author jinhu
 * created 2021-07-27 13:54
 */
public class Solution_0599 {
    public String[] findRestaurant(String[] list1, String[] list2) {
        HashMap<String, Integer> res00 = new HashMap<>();
        HashMap<String, Integer> res01 = new HashMap<>();
        for (int i = 0; i < list1.length; i++) {
            res00.put(list1[i], i);
        }
        for (int i = 0; i < list2.length; i++) {
            res01.put(list2[i], i);
        }
        ArrayList<String> res03 = new ArrayList<>();
        int min = Integer.MAX_VALUE;
        for (String v0 : res00.keySet()
        ) {
            if (res01.containsKey(v0)) {
                Integer b = res00.get(v0);
                Integer a = res01.get(v0);
                if (a + b < min) {
                    min = a + b;
                    res03.clear();
                    res03.add(v0);
                } else if (a + b == min) {
                    res03.add(v0);
                }
            }
        }
        String[] strings = new String[res03.size()];
        for (int i = 0; i < res03.size(); i++) {
            strings[i] = res03.get(i);
        }
        return strings;
    }
}
